import { defineComponent, reactive, ref } from 'vue'
import {
  queryCorrectionErrorListApi,
  queryKnowledgeListApi,
  queryIntentionTagListApi,
} from '@/api/ai-agent/detail'
import Model from './components/model'
import GenerateDiversity from './components/generate-diversity'
import Voice from './components/voice'
import { useUserStore } from '@/stores/user'

export default defineComponent({
  name: 'baseConfig',
  setup() {
    const user = useUserStore()
    const model = reactive({
      correctWordGroupId: [],
      knowledgeBaseId: '',
      intentionLabelGroupId: '',
      model: {},
      generateDiversity: {},
      voice: {},
    })

    const correctWordOptions = ref([])
    const knowledgeBaseOptions = ref([])
    const intentionLabelOptions = ref([])

    const queryCorrectionErrorList = async () => {
      try {
        const params = {
          companyId: user.companyId,
          type: 2,
        }
        const { data } = await queryCorrectionErrorListApi.getData(params)
        correctWordOptions.value = queryCorrectionErrorListApi.handleData(data)
      } catch (error) {
        console.error('获取纠错词列表失败', error)
      }
    }

    const queryKnowledgeList = async () => {
      const params = {
        companyId: user.companyId,
        type: 2,
      }
      const {
        data: { classifyList },
      } = await queryKnowledgeListApi(params)
      knowledgeBaseOptions.value = classifyList
    }

    const queryIntentionTagList = async () => {
      try {
        const params = {
          companyId: user.companyId,
          type: 1,
        }
        const { data } = await queryIntentionTagListApi(params)
        intentionLabelOptions.value = data
      } catch (error) {
        console.error('获取意向标签列表失败', error)
      }
    }

    const init = () => {
      queryCorrectionErrorList()
      queryKnowledgeList()
      queryIntentionTagList()
    }

    init()

    return () => (
      <>
        <div class="pa-[0_32px_0] mt-32px">
          <p class="mb-32px">·基本配置</p>
          <a-form model={model} layout="vertical">
            <a-form-item label="纠错词">
              <a-checkbox-group
                v-model:value={model.correctWordGroupId}
                options={correctWordOptions.value}
              />
            </a-form-item>
            <a-form-item label="知识库">
              <a-select v-model:value={model.knowledgeBaseId}>
                {knowledgeBaseOptions.value.map((i) => (
                  <a-select-option value={i.repositoryId}>{i.classifyName}</a-select-option>
                ))}
              </a-select>
            </a-form-item>
            <a-form-item label="意向标签组">
              <a-select v-model:value={model.intentionLabelGroupId}>
                {intentionLabelOptions.value.map((i) => (
                  <a-select-option value={i.id}>{i.groupName}</a-select-option>
                ))}
              </a-select>
            </a-form-item>
            <a-form-item label="选择模型">
              <Model modelValue={model.model} />
            </a-form-item>
            <a-form-item>
              <GenerateDiversity modelValue={model.generateDiversity} />
            </a-form-item>
            <a-form-item label="声音">
              <Voice modelValue={model.voice} />
            </a-form-item>
          </a-form>
        </div>
      </>
    )
  },
})
